import cv2
import numpy as np
import math


def zoomImg(img, zoom_multiple):
    height, width, channels = img.shape
    print("origin height=", height, "width=", width)
    new_height = int(height * zoom_multiple)
    new_width = int(width * zoom_multiple)
    print("new height=", new_height, "width=", new_width)
    emptyImage = np.zeros((new_height, new_width, channels), np.uint8)
    value = [0, 0, 0]
    # debug_count = 0
    for i in range(new_height):
        for j in range(new_width):
            origin_h = i/zoom_multiple
            origin_w = j/zoom_multiple
            floor_h = int(math.floor(origin_h))
            ceil_h = int(min(math.ceil(origin_h), height - 1))
            floor_w = int(math.floor(origin_w))
            ceil_w = int(min(math.ceil(origin_w), width - 1))
            for k in range(3):
                tmp1 = (origin_h - floor_h)*img[ceil_h, floor_w][k] + \
                    (1-(origin_h - floor_h))*img[floor_h, floor_w][k]
                tmp2 = (origin_h - floor_h)*img[ceil_h, ceil_w][k] + \
                    (1-(origin_h - floor_h))*img[floor_h, ceil_w][k]
                value[k] = (ceil_w - origin_w) * tmp1 + \
                    (1-(ceil_w - origin_w)) * tmp2
                value[k] = int(value[k])

            emptyImage[i, j] = (value[0], value[1], value[2])
            # debug_count += 1
    # print("debug-count=", debug_count)
    return emptyImage


img = cv2.imread("./GK8.png")
zoomed_img = zoomImg(img, 3.7)  # 放大倍数
cv2.imshow("bilinear", zoomed_img)
cv2.imshow("image", img)
cv2.waitKey(0)
